import { message } from "antd";
import Upload, { RcFile } from "antd/es/upload";

export type IUploadFileRestrictionsType = {
    size?: number;
    fileName?: string[];
}

/**
 * antd - 上传文件限制
 *
 */
export const handleUploadFileRestrictions = (file: RcFile, data: IUploadFileRestrictionsType) => {
    let show = true;
    //  判断文件大小
    if(data.size) {
        let num = Math.ceil(file.size / 1024 / 1024)
        if (num > data.size) {
            message.error(`文件大小不能超过${data.size}M`);
            show = false;
        }else{
            show = true;
        }
    }
    //  判断文件类型
    data.fileName?.forEach(element => {
        if( file.type === element) {
            show = false
        }
    });
    return show ||  Upload.LIST_IGNORE;
}